﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestMatrix.Model;

namespace TestMatrix.BLController
{
    public class FilterSettingController
    {
        private string _dateFilterType;
        private IsolatedStorageSettings userSettings = IsolatedStorageSettings.ApplicationSettings;

        public FilterSettingController(string dateFilterType)
        {
            _dateFilterType = dateFilterType;
        }

        public string DisplayRefreshDuration()
        {
            string outputStr = "";
            string lastRefresh="";

            try
            { 
                lastRefresh = (string)userSettings[ConstantDefinition.LAST_REFRESHED_DATE];
            }
            catch (System.Collections.Generic.KeyNotFoundException)
            {
                lastRefresh = DateTime.Now.ToString();
            }


            switch (_dateFilterType)
            {
                case "All":
                    outputStr = "All                                   " + lastRefresh;
                    break;

                case "Last 30 Days":
                    outputStr = "Last 30 Days                  " + lastRefresh;
                    break;

                case "Last 6 Months":
                    outputStr = "Last 6 Months               " + lastRefresh;

                    break;
                case "Last 12 Months":
                    outputStr = "Last 12 Months             " + lastRefresh;
                    break;

            }
            return outputStr;
        }

        public string AppendDateFilter(string inputStr)
        {
            string outputStr = "";

            if (string.IsNullOrEmpty(inputStr))
            {
                return null;
            }

            switch (_dateFilterType)
            {
                case "All":
                    outputStr = inputStr;
                    break;

                case "Last 30 Days":
                    outputStr = inputStr.Replace("where 1=1 ", "where CreatedDate >= datetime('now','-30 day') ");
                    break;

                case "Last 6 Months":
                    outputStr = inputStr.Replace("where 1=1 ", "where CreatedDate >= datetime('now','-6 month') ");

                    break;
                case "Last 12 Months":
                    outputStr = inputStr.Replace("where 1=1 ", "where CreatedDate >= datetime('now','-1 year') ");
                    break;

            }
            return outputStr;
        }

        /// <summary>
        /// used to append Multiple conditions 
        /// </summary>
        /// <param name="items"></param>
        /// <param name="inputStr"></param>
        /// <returns></returns>
        public static string AppendMultipleConditions(IList items,string inputStr)
        {
            string outputStr = "";

            if (items != null)
            {
                if (!string.IsNullOrWhiteSpace(inputStr))
                {
                    outputStr = inputStr;

                    foreach (var item in items)
                    {
                        outputStr += "'" + item + "',";
                    }
                    outputStr = outputStr.Substring(0, outputStr.Length - 1);
                    outputStr += ")";
                }
            }
            return outputStr;
        }
    }
}
